#include "WallpaperService.h"

WallpaperService::WallpaperService()
{
    this->url = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=%1&n=%2";
    this->httpClient = new HttpClient();
    QObject::connect(httpClient,SIGNAL(GetStringFinished(QString)),this,SLOT(OnGetBingWallpapersFinish(QString)));
}

void WallpaperService::GetBingWallpapersAsync(int index, int count)
{
    httpClient->GetStringAsync(this->url.arg((index-1)*count+1).arg(count));
}

QList<WallpaperModel*> WallpaperService::GetBingWallpapers(int index,int count)
{
    qDebug()<<"WallpaperService::GetBingWallpapers";

    QString str = httpClient->GetString(this->url.arg((index-1)*count+1).arg(count));

    QList<WallpaperModel *> localDeserialize = JsonHelper::Deserialize(str);
    qDebug()<<localDeserialize.size();

    for(int i =0;i<localDeserialize.size();i++)
    {
        WallpaperModel* paper = ((WallpaperModel*)localDeserialize.at(i));

        qDebug()<<paper->CopyRight;
    }
    return localDeserialize;
}

void WallpaperService::OnGetBingWallpapersFinish(QString str)
{
    QList<WallpaperModel*> papers = JsonHelper::Deserialize(str);
    qDebug()<<"WallpaperService::OnGetBingWallpapersFinish "<<papers.size();
    emit GetBingWallpapersFinish(papers);
}
